package com.macro.mall.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * 小红书订单详情视图对象
 * Created by AI on 2025/09/15.
 */
@Data
@ApiModel("小红书订单详情视图对象")
public class XhsOrderDetailVO {
    
    @ApiModelProperty("订单ID")
    private Long id;
    
    @ApiModelProperty("外部订单ID")
    private String outOrderId;
    
    @ApiModelProperty("小红书订单ID")
    private String xhsOrderId;
    
    @ApiModelProperty("用户手机号")
    private String phone;
    
    @ApiModelProperty("用户OpenID")
    private String openId;
    
    @ApiModelProperty("小程序AppID")
    private String appId;
    
    @ApiModelProperty("商品ID")
    private Integer productId;
    
    @ApiModelProperty("商品名称")
    private String productName;
    
    @ApiModelProperty("商品图片")
    private String productImage;
    
    @ApiModelProperty("SKU ID")
    private Integer skuId;
    
    @ApiModelProperty("订单原价(分)")
    private Long orderPrice;
    
    @ApiModelProperty("订单原价(元)")
    private String orderPriceYuan;
    
    @ApiModelProperty("订单实付金额(分)")
    private Long finalPrice;
    
    @ApiModelProperty("订单实付金额(元)")
    private String finalPriceYuan;
    
    @ApiModelProperty("运费(分)")
    private Long freightPrice;
    
    @ApiModelProperty("运费(元)")
    private String freightPriceYuan;
    
    @ApiModelProperty("优惠金额(分)")
    private Long discountPrice;
    
    @ApiModelProperty("优惠金额(元)")
    private String discountPriceYuan;
    
    @ApiModelProperty("支付方式代码")
    private Integer payChannel;
    
    @ApiModelProperty("支付方式文本")
    private String payChannelText;
    
    @ApiModelProperty("订单状态")
    private Integer orderStatus;
    
    @ApiModelProperty("显示状态文本")
    private String displayStatusText;
    
    @ApiModelProperty("状态标签颜色")
    private String statusColor;
    
    @ApiModelProperty("是否已过期")
    private Boolean isExpired;
    
    @ApiModelProperty("券码")
    private String voucherCode;
    
    @ApiModelProperty("券码二维码图片地址")
    private String voucherCodeImage;
    
    @ApiModelProperty("支付Token")
    private String payToken;
    
    @ApiModelProperty("门店ID")
    private String storeId;
    
    @ApiModelProperty("小程序页面路径")
    private String path;
    
    @ApiModelProperty("支付类型")
    private String openPayType;
    
    @ApiModelProperty("回调状态")
    private Integer callbackStatus;
    
    @ApiModelProperty("支付时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date payTime;
    
    @ApiModelProperty("支付过期时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date payExpiredTime;
    
    @ApiModelProperty("订单过期时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date orderExpiredTime;
    
    @ApiModelProperty("业务创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date bizCreateTime;
    
    @ApiModelProperty("业务更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date bizUpdateTime;
    
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;
    
    @ApiModelProperty("更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;
    
    @ApiModelProperty("核销时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date verifyTime;
    
    /**
     * 设置各种金额字段并自动计算元格式
     */
    public void setOrderPrice(Long orderPrice) {
        this.orderPrice = orderPrice;
        this.orderPriceYuan = convertToYuan(orderPrice);
    }
    
    public void setFinalPrice(Long finalPrice) {
        this.finalPrice = finalPrice;
        this.finalPriceYuan = convertToYuan(finalPrice);
    }
    
    public void setFreightPrice(Long freightPrice) {
        this.freightPrice = freightPrice;
        this.freightPriceYuan = convertToYuan(freightPrice);
    }
    
    public void setDiscountPrice(Long discountPrice) {
        this.discountPrice = discountPrice;
        this.discountPriceYuan = convertToYuan(discountPrice);
    }
    
    /**
     * 分转元的通用方法
     */
    private String convertToYuan(Long priceInCent) {
        if (priceInCent == null) {
            return "0.00";
        }
        BigDecimal yuan = new BigDecimal(priceInCent).divide(new BigDecimal(100), 2, RoundingMode.HALF_UP);
        return yuan.toString();
    }
    
    /**
     * 设置支付方式并自动设置文本描述
     */
    public void setPayChannel(Integer payChannel) {
        this.payChannel = payChannel;
        if (payChannel != null) {
            switch (payChannel) {
                case 1:
                    this.payChannelText = "微信";
                    break;
                case 2:
                    this.payChannelText = "支付宝";
                    break;
                case 99:
                    this.payChannelText = "其它";
                    break;
                default:
                    this.payChannelText = "未知";
                    break;
            }
        }
    }
    
    /**
     * 设置订单状态并自动设置显示文本和颜色
     */
    public void setDisplayStatus(Integer orderStatus, Boolean isExpired) {
        this.orderStatus = orderStatus;
        this.isExpired = isExpired;
        
        if (isExpired != null && isExpired) {
            this.displayStatusText = "已过期";
            this.statusColor = "info";
        } else if (orderStatus != null) {
            switch (orderStatus) {
                case 0:
                    this.displayStatusText = "待支付";
                    this.statusColor = "warning";
                    break;
                case 1:
                    this.displayStatusText = "支付成功";
                    this.statusColor = "primary";
                    break;
                case 2:
                    this.displayStatusText = "支付失败";
                    this.statusColor = "danger";
                    break;
                case 3:
                    this.displayStatusText = "已取消";
                    this.statusColor = "info";
                    break;
                case 4:
                    this.displayStatusText = "已退款";
                    this.statusColor = "danger";
                    break;
                case 5:
                    this.displayStatusText = "已完成";
                    this.statusColor = "success";
                    break;
                default:
                    this.displayStatusText = "未知状态";
                    this.statusColor = "info";
                    break;
            }
        }
    }
}
